# 模块名和模块测试APP名称
MODULE_NAME       := printk_prj_info

ARCH              ?= arm
MAKE_PARAM        :=
CURRENT_PATH      := $(shell pwd)
KERNEL_KO_RELEASE  = $(shell git rev-parse --verify --short HEAD)
ifeq ("$(origin V)", "command line")
  KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE = 0
endif

ifeq ($(KBUILD_VERBOSE),1)
  quiet =
  Q =
else
  quiet=quiet_
  Q = @
endif

#=====================================================
INCDIRS 		  := ./ 			   
SRCDIRS			  := ./

INCLUDE			  := $(patsubst %, -I %, $(INCDIRS))
#=====================================================
# NFS 共享目录
TFTP_SERVER       ?= ~/3tftp
NFS_SERVER        ?= ~/4nfs

TFTP_DIR          ?= $(TFTP_SERVER)
ROOTFS_ROOT_DIR   ?= $(NFS_SERVER)/imx6ull_rootfs
#ROOTFS_MODULE_DIR ?= $(ROOTFS_ROOT_DIR)/lib/modules/4.19.71-00007-g51f3cd8ec-dirty
ROOTFS_MODULE_DIR ?= $(ROOTFS_ROOT_DIR)/drivers_demo
#=====================================================
ifeq ($(KERNELRELEASE),)

# 选择可执行文件运行的平台
ifeq ($(ARCH), arm)
KERNELDIR            ?= ~/7Linux/imx6ull-kernel 
MAKE_PARAM           += ARCH=arm
MAKE_PARAM           += CROSS_COMPILE=arm-linux-gnueabihf-
CROSS_COMPILE_PREFIX ?= arm-linux-gnueabihf-
else
KERNELDIR            ?= /lib/modules/$(shell uname -r)/build
CROSS_COMPILE_PREFIX ?=
endif

CC                   := $(CROSS_COMPILE_PREFIX)gcc
LD                   := $(CROSS_COMPILE_PREFIX)ld

include Kbuild.include

srctree              := .
timestamp_h          := timestamp_autogenerated.h
version_h            := version_autogenerated.h

export CC LD srctree

# 编译模块和测试程序
all: $(timestamp_h) $(version_h) modules

modules:
	$(MAKE) $(MAKE_PARAM) -C $(KERNELDIR) M=$(CURRENT_PATH) modules $(INCLUDE)

modules_install:
	$(MAKE) $(MAKE_PARAM) -C $(KERNELDIR) M=$(CURRENT_PATH) modules INSTALL_MOD_PATH=$(ROOTFS_MODULE_DIR) modules_install

$(timestamp_h): $(srctree)/Makefile FORCE
	$(call filechk,timestamp_autogenerated.h)
	
$(version_h): $(srctree)/Makefile FORCE
	$(call filechk,version_autogenerated.h)

# 拷贝相关文件到nfs共享目录
install:
	@sudo cp -v $(MODULE_NAME).ko $(ROOTFS_MODULE_DIR)

uninstall:
	@sudo rm -vf $(ROOTFS_MODULE_DIR)/$(MODULE_NAME).ko 

PHONY += FORCE
FORCE:

PHONY += clean
clean:
	rm -rf *.o *.ko *.o.d
	rm -rf .*.cmd  *.mod.* *.mod modules.order Module.symvers .tmp_versions .cache.mk 
	rm -rf $(timestamp_h)
	rm -rf $(version_h)

.PHONY: $(PHONY)

help:
	@echo "\033[1;32m================================ Help ================================\033[0m"
	@echo "Ubuntu may need to add sudo:"
	@echo "insmod <module_name>.ko   # Load module"
	@echo "rmmod <module_name>       # Uninstall the module"
	@echo "dmesg -C                  # Clear the kernel print information"
	@echo "lsmod                     # Check the kernel modules that have been inserted"
	@echo "dmesg                     # View information printed by the kernel"
	@echo "file <module_name>.ko     # View \".Ko\" file information"
	@echo ""
	@echo "make ARCH=x86_64          # x86_64 platform"
	@echo "make                      # arm platform"
	@echo "\033[1;32m======================================================================\033[0m"

print:
	@echo "KERNELDIR = $(KERNELDIR)"
	@echo "INCLUDE   = $(INCLUDE)"
else
CONFIG_MODULE_SIG = n
obj-m            += $(MODULE_NAME).o
endif